package com.jhh.pay.center.service.impl;

import com.jhh.pay.center.cfg.caching.Cache;
import com.jhh.pay.center.dao.AppChannelMapper;
import com.jhh.pay.center.model.AppChannel;
import com.jhh.pay.center.model.AppChannelValuePair;
import com.jhh.pay.center.service.AppChannelService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.concurrent.TimeUnit;

/**
 * @author tangxd
 * @Description: TODO
 * @date 2017/12/5
 */
@Service
@Slf4j
public class AppChannelServiceImpl implements AppChannelService {

    @Autowired
    private AppChannelMapper appChannelMapper;

    @Autowired
    private Cache cache;

    private String cacheKey(int appId, int channelId) {
        return String.format("jhh:pay:app_channel:%d_%d", appId, channelId);
    }

    private String cacheKeyListByAppId(int appId) {
        return String.format("jhh:pay:app_channel_list_appId:%d", appId);
    }

    private String cacheKeyListByChannelId(int channelId) {
        return String.format("jhh:pay:app_channel_list_channelId:%d", channelId);
    }

    @Override
    public boolean save(AppChannel ac) {
        AppChannel appc = get(ac.getAppId(), ac.getChannelId());
        if (appc != null) {
            return false;
        }
        boolean ok = appChannelMapper.insert(ac) > 0;
        if (ok) {
            cache.set(cacheKey(ac.getAppId(), ac.getChannelId()), ac, TimeUnit.SECONDS.convert(3, TimeUnit.DAYS));
            cache.del(cacheKeyListByAppId(ac.getAppId()));
            cache.del(cacheKeyListByChannelId(ac.getChannelId()));
        }
        return ok;
    }

    @Override
    public boolean update(AppChannel ac) {
        boolean ok = appChannelMapper.update(ac) > 0;
        if (ok) {
            cache.set(cacheKey(ac.getAppId(), ac.getChannelId()), ac, TimeUnit.SECONDS.convert(3, TimeUnit.DAYS));
        }
        return ok;
    }

    @Override
    public boolean delete(int appId, int channelId) {
        boolean ok = appChannelMapper.delete(appId, channelId) > 0;
        if (ok) {
            cache.del(cacheKey(appId, channelId));
            cache.del(cacheKeyListByAppId(appId));
            cache.del(cacheKeyListByChannelId(channelId));
        }
        return ok;
    }

    @Override
    public AppChannel get(int appId, int channelId) {
        AppChannel ac = cache.get(cacheKey(appId, channelId), AppChannel.class);
        if (null == ac) {
            ac = appChannelMapper.get(appId, channelId);
            if (null != ac) {
                cache.set(cacheKey(appId, channelId), ac, TimeUnit.SECONDS.convert(3, TimeUnit.DAYS));
            }
        }
        return ac;
    }

    @Override
    public Collection<AppChannel> getsByAppId(int appId) {
        Collection<AppChannelValuePair> vals = cache.getList(cacheKeyListByAppId(appId), AppChannelValuePair.class);
        List<AppChannel> acs = new ArrayList<>();
        if (vals.size() == 0) {
            vals = appChannelMapper.getsByAppId(appId);
            cache.set(cacheKeyListByAppId(appId), vals, TimeUnit.SECONDS.convert(5, TimeUnit.DAYS));
        }
        for (AppChannelValuePair vp : vals) {
            AppChannel ac = get(vp.getVal1(), vp.getVal2());
            acs.add(ac);
        }
        return acs;
    }

    @Override
    public Collection<AppChannel> getsByChannelId(int channelId) {
        Collection<AppChannelValuePair> vals = cache.getList(cacheKeyListByChannelId(channelId), AppChannelValuePair.class);
        List<AppChannel> acs = new ArrayList<>();
        if (vals.size() == 0) {
            vals = appChannelMapper.getsByChannelId(channelId);
            cache.set(cacheKeyListByChannelId(channelId), vals, TimeUnit.SECONDS.convert(5, TimeUnit.DAYS));
        }
        for (AppChannelValuePair vp : vals) {
            AppChannel ac = get(vp.getVal1(), vp.getVal2());
            acs.add(ac);
        }
        return acs;
    }
}
